let {
  getResourceList,
  getResourceListPage,
  InsertData
} =require ("../../../api/slibrary.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: wx.getStorageSync('userInfoMy'),
    showcontent: false,
    data_slibray: {
      zyjb: '一级资源',
      bm: '',
      zylx: '重点企业',
      qy: '苏州',
      zymc: '',
      lxr: "",
      lxrzw: '',
      zynl: '',
      hzsq: '',
      hzlx: '',
      hzkd: '',
      djr: '',
      djxm: '',
      hzjz: '',
      bz: '',
      tname: '',
      type: 1
    },
    showcontent: false,
    select_type: 'zyjb',
    zyjb_select: [{
      text: '一级资源'
    }, {
      text: '二级资源'
    }, {
      text: '三级资源'
    }],
    zylx_select: [],
    types: [{
      text: '全部',
      id: 1,
      fontWeight: '600',
      color: '#333'
    },
    {
      text: '大院大所',
      id: 2,
      fontWeight: '600',
      color: '#666'
    },
    {
      text: '产业基金',
      id: 3,
      fontWeight: '600',
      color: '#666'
    },
    {
      text: '服务机构',
      id: 3,
      fontWeight: '600',
      color: '#666'
    },
    {
      text: '协会/校友会',
      id: 3,
      fontWeight: '600',
      color: '#666'
    },
    {
      text: '重点企业',
      id: 3,
      fontWeight: '600',
      color: '#666'
    },
    {
      text: '政府资源',
      id: 3,
      fontWeight: '600',
      color: '#666'
    },
    {
      text: '其他资源',
      id: 3,
      fontWeight: '600',
      color: '#666'
    },
    ],
    type: [{
      text: '全部',
      id: 1,
      fontWeight: '600',
      color: '#333'
    },
    {
      text: '部门资源',
      id: 2,
      fontWeight: '600',
      color: '#666'
    },
    {
      text: '资源统计',
      id: 3,
      fontWeight: '600',
      color: '#666'
    },
    ],
    tableList: [],
    params: {
      type: '全部',
      pageSize: 12,
      pageNumber: 1,
      value: '',
      department: ''
    },
    num: '',
    total: '',
    value: '',
    show: false
  },

  clickcontent(e) {
    let key = `data_slibray.${this.data.select_type}`;
    this.setData({
      [key]: e.currentTarget.dataset.content,
    })
    this.onClickcontent()
  },
  onClickcontent() {
    this.setData({
      showcontent: !this.data.showcontent
    })

  },
  inputValue(e) {
    let param = e.currentTarget.dataset.name; //字段名
    let value = e.detail; //值
    let key = `data_slibray.${param}`;
    if (param == 'zyjb' || param == 'zylx') {
      if (param == 'zylx') {
        this.setData({
          zylx_select: this.data.types.slice(1),
          select_type: param
        })
      } else {
        this.setData({
          select_type: param
        })
      }
      console.log(this.data.zylx_select)
      setTimeout(() => {
        this.setData({
          showcontent: true,
        })
      }, 500)
    } else {
      this.setData({
        [key]: value,
      });
    }

  },
  onSubmit() {
    console.log(this.data.data_slibray)
    if (this.data.data_slibray.zylx != '' && this.data.data_slibray.zyjb != '' && this.data.data_slibray.zymc != '' && this.data.data_slibray.qy != '' && this.data.data_slibray.zynl != '') {
      InsertData(this.data.data_slibray).then(res => {
        if (res.status == 500) {
          return wx.showToast({
            title: '对接人非本公司',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '添加完成',
            icon: 'none'
          }),
            setTimeout(() => {
              this.onClose()
              this.onLoad()
            }, 300)
        }
      })
    } else {
      wx.showToast({
        title: '请补充必填项',
        icon: 'none'
      })
    }
  },
  onClose() {
    this.setData({
      show: !this.data.show,
      data_slibray: {
        zyjb: '一级资源',
        bm: this.data.userInfo.bm,
        zylx: '重点企业',
        qy: '',
        zymc: '',
        lxr: "",
        lxrzw: '',
        zynl: '',
        hzsq: '',
        hzlx: '',
        hzkd: '',
        djr: this.data.userInfo.jname,
        djxm: '',
        hzjz: '',
        bz: '',
        tname: this.data.userInfo.jname,
        type: 1

      },
    })
  },
  onSelect(event) {
    let {
      index,
      item
    } = event.currentTarget.dataset;
    let tabs = this.data.types
    for (let i = 0; i < tabs.length; i++) {
      if (i === index) {
        tabs[i].color = '#333';
      } else {
        tabs[i].color = '#666';
      }
    }
    this.setData({
      types: tabs
    });
    this.setData({
      [`params.type`]: item.text
    })
    this.setData({
      [`params.pageNumber`]: 1,
      num: 1
    })
    this.getNewList()
    this.getNewPage()
  },
  onSelect1(event) {
    let {
      index,
      item
    } = event.currentTarget.dataset;
    let tabs = this.data.type
    for (let i = 0; i < tabs.length; i++) {
      if (i === index) {
        tabs[i].color = '#333';
      } else {
        tabs[i].color = '#666';
      }
    }
    this.setData({
      type: tabs
    });
    let str = `联健元和,达博,科澜项目,国科项目,江环项目,创界项目,秦淮项目,黄埭项目,花桥项目,江宁项目,江北项目,西山项目,北联新翼,新零售,总裁办,双创中心,投资拓展中心,招商中心,文创,深圳项目,科技中心,运营服务中心`
    if (item.text == '部门资源') {
      this.setData({
        [`params.department`]: this.data.userInfo.xmgs || str
      })
    } else if (item.text == '全部') {
      this.setData({
        [`params.department`]: str
      })
    }
    this.setData({
      [`params.pageNumber`]: 1,
      num: 1
    })
    if (item.text !== '资源统计') {
      this.getNewList()
      this.getNewPage()
    } else {
      wx.navigateTo({
        url: '/subPackages/pages/slibrary/total/total',
      })
    }

  },
  getNewList() {
    getResourceList(this.data.params).then(res => {
      this.setData({
        tableList: res.data
      })

    })
  },
  getNewPage() {
    getResourceListPage(this.data.params).then(res => {
      this.setData({
        total: Math.round(res.data[0].total / (this.data.params.pageSize))
      })
    })
  },
  onChange(event) {
    this.setData({
      value: event.detail,
      [`params.value`]: event.detail
    })
    if (this.data.value == '') {
      this.setData({
        [`params.pageNumber`]: 1,
        num: 1,
      })
      this.getNewList()
      this.getNewPage()
    }
  },
  onSearch() {
    this.setData({
      [`params.pageNumber`]: 1,
      num: 1
    })
    this.getNewList()
    this.getNewPage()
  },
  onChange1() {
    if (this.data.num > 1) {
      this.setData({
        num: this.data.num - 1,
        [`params.pageNumber`]: this.data.num - 1
      })
      this.getNewList()
    }
  },
  onChange2() {
    if (this.data.num < this.data.total) {
      this.setData({
        num: this.data.num + 1,
        [`params.pageNumber`]: this.data.num + 1
      })
      this.getNewList()
    }
  },
  onDetail(e) {
    wx.navigateTo({
      url: `../detail/detail?zyID=${e.currentTarget.dataset.zyid}`,
    })
  },
  onLoad(options) {
    this.setData({
      num: this.data.params.pageNumber,
      userInfo: wx.getStorageSync('userInfoMy'),
      [`params.department`]: `联健元和,达博,科澜项目,国科项目,江环项目,创界项目,秦淮项目,黄埭项目,花桥项目,江宁项目,江北项目,西山项目,北联新翼,新零售,总裁办,双创中心,投资拓展中心,招商中心,文创,深圳项目,科技中心,运营服务中心`
    })
    this.getNewList()
    this.getNewPage()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})